package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.ScoreMapper;
import top.maof.book.model.Score;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.ScoreService;
import top.maof.book.utils.Utils;

@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public PageData<Score> findPage(PageWrap<Score> pageWrap) {
        IPage<Score> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Score::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(Score::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getBookId() != null) {
            queryWrapper.lambda().eq(Score::getBookId, pageWrap.getModel().getBookId());
        }
        if (pageWrap.getModel().getTime() != null) {
            queryWrapper.lambda().eq(Score::getTime, pageWrap.getModel().getTime());
        }
        if (pageWrap.getModel().getScore() != null) {
            queryWrapper.lambda().eq(Score::getScore, pageWrap.getModel().getScore());
        }
        for (PageWrap.SortData sortData : pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(scoreMapper.selectPage(page, queryWrapper));
    }

    // 获取书的评分
    @Override
    public double bookScore(int bookId) {
        Double score = scoreMapper.bookScore(bookId);
        return score == null ? 0 : score.doubleValue();
    }

    // 获取用户对书的评分
    @Override
    public Score findScoreByUserAndBook(int userId, int bookId) {
        return scoreMapper.findScoreByUserAndBook(userId, bookId);
    }

    // 添加纪录
    @Override
    public int add(Score score) {
        if (this.findScoreByUserAndBook(score.getUserId(), score.getBookId()) == null)
            return scoreMapper.add(score);
        else
            return 0;
    }


    // 评分,没有纪录则添加,有则修改
    @Override
    public int evaluateScore(Score score) {
        if (this.findScoreByUserAndBook(score.getUserId(), score.getBookId()) == null)
            return scoreMapper.add(score);
        else
            return scoreMapper.updateScore(score);
    }
}
